import publicForm from "@/components/public/el-forms.vue"; //弹框form
import formJs from "./regexDetail.js"; //新增弹框配置项
import CustomDialog from "@/components/inspectionManagementManage/inspectionReport/WBS/index.vue";
import publicForm1 from "@/components/public/el-form.vue"; //弹框form
import FormLyout from '@/components/commonPublic/fromLayout.vue'
import actions from "@/actions";

import {

    saveSiteInspection, //保存
    getSiteInspection, //详情

    getEntityList, //构件类型
    getEntityDetectionList, //检测项目
    // getStoragelocationByCompanyId, //存放位置
    // getPurveyorDataList,
    // getSpecificationDataList
} from "@/api/request.js";
export default {

    data() {
        return {
            visible: "",
            loading: false,
            title: null,
            isLock: false,
            tableData: [],
            positionData1: [],
            selected: [], //多选人
            btnStatus: [],
            positionObj: [],
            dialogHeight: null,
            activeIndex: "1",
            formObj: formJs.formObj,
            additionalObj: formJs.additionalObj,
            isEdit: false,
            currntData: null,
            dataInfo: null,
            startDates: null,
            treeData: [], //材料种类
            formData: {
                fId: "", //主键id,没有为新增
                engineeringDivision: "", //工程划分
                engineeringId: "",
                structureName: "", //结构物名称
                part: "", //部位
                component: "", //构建
                componentType: "", //构建类型
                companyId: "", //标段id
                companyName: "", //标段名称
                note: "", //检测说明
                isIntact: "", //填报内容是否完整
                files: "", //附件资料
                userId: "", //填报人id
                userName: "", //填报人名称
                createDates: this.common.day(0),
                recordfiles: [ //需要删除图片信息的主键
                ],
                testItemList: []
            },
            formDatafileList: [], //工作计划资料
            btnShow: false,
            currentDetail: null,
            selectPostionObj: [],
            types: [],
            // 材料弹框
            materialRow: {},
            materialShow: false,

            fFileurllist: [], //附件
            primaryInfo: null,

        };
    },
    components: {
        publicForm,
        publicForm1,
        FormLyout,
        CustomDialog
    },


    mounted() {
        this.primaryInfo = JSON.parse(sessionStorage.getItem("GxEmbedded_primaryInfo"))
        actions.onGlobalStateChange((state, prev) => {
            var data = prev.currentUser.projects;
            this.userInfo = prev.currentUser;
        }, true);

        // this.formData.companyId= this.fCompanyid//标段id

        this.getEntityList() //构件类型
        if (this.primaryInfo.status == 1) { //新增
            this.btnShow = true

            this.formData.companyId = this.primaryInfo.fCompanyid;

            this.formData.files = 'file' + new Date().getTime();
            this.formObj.map((item) => {
                item.disabled = false;
            });
            
            this.isEdit = false;
            this.additionalObj.map((item) => {
                item.disabled = false;
            });
            this.additionalObj[1].uploadSelect = true;
            this.additionalObj[2].disabled = true;
            this.formObj[0].inputButton=true;
            this.formData.userName = this.userInfo.userName; //当前登录人名称
            this.formData.userId = this.userInfo.id; //当前登录人id

        } else if (this.primaryInfo.status == 2) { //编辑
            // currentRow
            this.formData.fId = this.primaryInfo.data.fId //按钮 
            this.getTender(this.primaryInfo.fprojectid);
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.formObj[0].disabled = false;
            this.formObj[2].disabled = false;
            this.formObj[6].disabled = false;
            this.formObj[7].disabled = false;
            this.formObj[8].disabled = false;
            this.formObj[10].disabled = false;
            this.isEdit = false;
            this.additionalObj.map((item) => {
                item.disabled = false;
            });
            this.additionalObj[1].uploadSelect = true;
            this.additionalObj[2].disabled = true;
            this.formObj[0].inputButton=true;
            this.btnShow = true
        } else if (this.primaryInfo.status == 7) { //查看
            this.btnShow = false
            this.getDetail({
                fId: this.primaryInfo.data.fId
            })
            this.formObj.map((item) => {
                item.disabled = true;
            });
            this.isEdit = true

            this.additionalObj.map((item) => {
                item.disabled = true;
            });
            this.additionalObj[1].uploadSelect = false;
         
            this.formObj[0].inputButton=false;
        }

    },
    methods: {
        handleSelect(key) {
            this.activeIndex = key;
        },
        async getTender(e) {
            await this.$http(
                " http://172.18.5.31:8300/IOTManagement/Base/Company/getGXJLBaseCompanyList.do?projectId=" +
                e, {
                    headers: {
                        "Content-Type": "application/json",
                        Authorization: localStorage.getItem("access_token"),
                    },
                }
            ).then((res) => {
                if (res.status == 200) {
                    var data = res.data.data.GXCompanyList;


                }
            });
        },
        clickDialog(index, prop) {
            console.log(index, prop);
            if (index == 0 && prop == "engineeringDivision") {
                this.materialShow = true;
                // this.getDataList();
            }
        },
        getWBS(data) { //获取选择WBS信息
            // console.log(data);
            // console.log(444,data[0].ShortName.split("/")[0],9999,data[data.length-1].Name);

            var name = [];
            var id = [];
            data.map(item => {
                name.push(item.ShortName);
                id.push(item.LongIds);
            })
            console.log(data, name, id);
            this.formData.engineeringDivision = name.join(','); //工程部位
            this.formData.engineeringId = id.join(','); //工程部位id
            this.formData.structureName=data[0].ShortName.split("/")[0]+'-'+data[data.length-1].Name;
            // if()
        },
        selectChange(data, a, b) {
            if (b == "componentType") { //构件类型
                this.formData.componentType = data.label; //生产厂家
                this.formData.componentId = data.value; //生产厂家id
                this.getEntityDetectionList(data.value)
            }
        },
        getEntityDetectionList(id) {
            getEntityDetectionList({
                    page: 1,
                    pageSize: 500,
                    fCompanyid: id
                })
                .then((res) => {
                    this.tableData = res.data.pageInfo.list;
                })
        },

        edit() {
            this.formData = this.$options.data.call(this).formData;
        },

        // 构件类型
        getEntityList() {

            getEntityList({})
                .then((res) => {
                    if (res.errcode == 0) {
                        this.formObj[1].options = []
                        res.data[0].children.map(item => {
                            this.formObj[1].options.push({
                                label: item.structureName,
                                value: item.fId,
                            })
                        })
                    } else {

                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        return false;
                    }
                })
                .catch(() => {
                    return false;
                });
        },
        // 刪除文件
        delteFile(file) {
            for (let index = 0; index < this.fFileurllist.length; index++) {
                if (file.uid == this.fFileurllist[index].uid) {
                    if (this.fFileurllist[index].atta == undefined) {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].fId,
                        });
                    } else {
                        this.formData.recordfiles.push({
                            fId: this.fFileurllist[index].atta.fId,
                        });
                    }
                    this.fFileurllist.splice(index, 1);
                }
            }
        },
        // 详情
        getDetail(obj) {
            this.loading = true;
            getSiteInspection(obj).then((res) => {
                if (res.errcode == 0) {
                    this.loading = false;
                    var data = res.data.yjQmpClJlsiteinspection;
                    // this.getEntityDetectionList(data.componentId)
                    this.formData.recordfiles = [];
                    this.formData = data;
                    this.fFileurllist = res.data.filesList;
                    this.tableData = data.testItemList;
                    // if (data.createDates == null) {
                    //     this.formData.userName = this.userInfo.userName; //当前登录人名称
                    //     this.formData.userId = this.userInfo.id; //当前登录人id
                    // }

                    if (this.primaryInfo.status == 7) {
                        this.btnShow = false;
                    } else {
                        this.btnShow = true;
                    }
                } else {
                    this.loading = false;
                    this.$message.error({
                        content: res.msg,
                        duration: 1000
                    })
                }
            });
        },
        yj_saveForm() {
            this.formData.userId = this.userInfo.id; //当前登录人id
            this.formData.userName = this.userInfo.userName; //当前登录人名称
            this.formData.testItemList = this.tableData;
            // 0连续新增1新增
            if (this.$refs.publicForm.submitForm()) {
                this.saveForm(this.formData);
            } else {
                console.log("error");
            }

        },
        // 保存
        saveForm(data) {
            this.loading = true;
            this.isLock = true
            saveSiteInspection(data)
                .then((res) => {
                    if (res.errcode == 0) {
                        this.loading = false;
                        this.visible = false;
                        this.$router.push({
                            path: "/inspectionManagementManage/inspectionReport",

                        });
                        window.sessionStorage.removeItem("GxEmbedded_primaryInfo");
                        this.$message.success({
                            content: "新增成功",
                            duration: 1000
                        })
                        this.isLock = false
                    } else {
                        this.loading = false;
                        this.isLock = false

                        this.$message.error({
                            content: res.msg,
                            duration: 1000
                        })
                        console.log("接口报错");
                    }
                })
                .catch((error) => {
                    console.log("拌合站设置-新增）", error);
                });
        },
        refreshList() {},
        materialDialog() {
            this.materialShow = false;
        },
    },
};